/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.business
{
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	
	import multimedia.model.ModelLocator;
	
	import mx.rpc.IResponder;
	
	public class Red5Connection
	{
		public static var SUCCESS:String = "success";
		public static var FAILED:String = "failed";
		public static var CLOSED:String = "closed";
		public static var REJECTED:String = "rejected";
		public static var INVALIDAPP:String = "invalidApp";
		public static var APPSHUTDOWN:String = "appShutdown";
		public static var SECURITYERROR:String = "securityError";
		public static var DISCONNECTED:String = "disconnected";
		
		private var nc:NetConnection;
		private var _uri:String = "";
		
		private var responder:IResponder;
		
		public function Red5Connection(responder:IResponder)
		{
			this.responder = responder;
			
			//  create the netConnection
			nc = new NetConnection();
			//  set it's client/focus to this
			nc.client = this;
			
			// add listeners for netstatus and security issues
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			
			// set the encoding to AMF0 - still waiting for AMF3 to be implemented on Red5
			nc.objectEncoding = ObjectEncoding.AMF0;
		}
		
		public function connect(uri:String):void
		{
			_uri = uri;
			nc.connect(_uri);
		}
		
		public function close():void
		{
			nc.close();
		}
				
		public function getNetConnection():NetConnection
		{
			return nc;	
		}
		
		public function isConnected():Boolean
		{
			return nc.connected;
		}
		
		public function onBWDone():void
		{
			// have to have this for an RTMP connection
		}
		
		private function netStatusHandler(event:NetStatusEvent):void 
		{			
			if(event.info.code == "NetConnection.Connect.Success")
			{				
				responder.result(event.info.code);
			}
			else
			{						
				responder.fault(event.info.code);
			}	
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			
		}
	}
}
